package com.eastcom.common.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class ContainerUtil {

	public static List getObjectsByValueMap(Map objectMap,Map valueMap){
		if(objectMap==null||objectMap.isEmpty())
			return null;
		
		List targets=new ArrayList();
		Iterator it=objectMap.values().iterator();
		Object obj=null;
		while(it.hasNext()){
			obj= it.next();
			if(isTargetObject(obj,valueMap))
				targets.add(obj);
		}
		
		return targets;
	}
	
	public static Object getUniqueReferenceObject(Map sourceClassCondition,Map sourceObjMap,Map referenceMap,Map targetObjMap){
		if(sourceObjMap!=null&&!sourceObjMap.isEmpty()){
			Object sourceObject=getUniqueObjectByValueMap(sourceObjMap,sourceClassCondition);
			if(sourceObject!=null){
				Map valueMap=getValueMap(sourceObject,referenceMap);
				if(valueMap!=null){
					if(targetObjMap!=null&&!targetObjMap.isEmpty()){
						return getUniqueObjectByValueMap(targetObjMap,valueMap);
					}
				}
			}
		}

		return null;
	}
	
	public static Object getUniqueObjectByValueMap(Map objects,Map valueMap){
		Iterator it=objects.values().iterator();
		Object obj=null;
		while(it.hasNext()){
			obj= it.next();
			if(isTargetObject(obj,valueMap))
				return obj;
		}
		
		return null;
	}
	
	protected static Map getValueMap(Object sourceObject,Map reflectMap){
		Map valueMap=new HashMap();
		Class clazz=sourceObject.getClass();
		Iterator it=reflectMap.entrySet().iterator();
		Method getterMethod=null;
		String []params=null;
		
		try{
		    while(it.hasNext()){
			    Entry entry=(Entry)it.next();
			    String targetField=(String) entry.getKey();
			    String sourceField=(String) entry.getValue();
			    params=sourceField.split("/");
			    
			    Object paramObject=sourceObject;
			    for(int i=0;i<params.length;i++){
			    	getterMethod=clazz.getMethod("get"+StringUtil.setFirstCharToUpperCase(params[i]), null);
			    	paramObject=getterMethod.invoke(paramObject, null);
			    	
			    	if(paramObject==null&&i!=params.length-1)
			    		break;

			    	clazz=paramObject.getClass();
			    }
			    
			    valueMap.put(targetField,paramObject);
			    clazz=sourceObject.getClass();
		    }
		    
		    return valueMap;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	protected static boolean isTargetObject(Object object,Map valueMap){
		if(valueMap==null||valueMap.isEmpty())
			return true;
		
		Iterator it=valueMap.entrySet().iterator();
		Entry entry=null;
		boolean isTarget=true;
		Method gettermethod=null;
		Class clazz=object.getClass();
		try{
		    while(it.hasNext()){
			    entry=(Entry) it.next();
			    String parametetName=(String) entry.getKey();
			    gettermethod=clazz.getMethod("get"+StringUtil.setFirstCharToUpperCase(parametetName), null);			
			    Object paravalue=gettermethod.invoke(object, null);
			    if(!entry.getValue().equals(paravalue)){
			    	isTarget=false;
			    	break;
			    }
		    }
		}
		catch(Exception e){
			e.printStackTrace();
			isTarget=false;
		}
		
		return isTarget;
	}
	
}
